//
//  DetailViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/14.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {
    weak var delegate: ComicViewWillEndDraggingDelegate?
    var detailStatic: DetailStaticModel?
    var detailRealtime: DetailRealtimeModel?
    var guessLike: LoveModel?
    
    fileprivate lazy var tableView: UITableView = {
       let tv = UITableView(frame: .zero, style: .plain)
        tv.separatorStyle  = .none
        tv.backgroundColor = kRGBA(r: 242, g: 242, b: 242, a: 1)
        tv.register(OtherWorkCell.self, forCellReuseIdentifier: "OtherWorkCell")
        tv.register(DescriptionCell.self, forCellReuseIdentifier: "DescriptionCell")
        tv.register(TicketCell.self, forCellReuseIdentifier: "TicketCell")
        tv.register(LoveComicsCell.self, forCellReuseIdentifier: "LoveComicsCell")
        tv.delegate = self
        tv.dataSource = self
        return tv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.tableView)
        setUpSnap()
        // Do any additional setup after loading the view.
    }
    
    fileprivate func setUpSnap() {
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.usnp.edges)
        }
    }

    func detailReloadData() {
        self.tableView.reloadData()
    }
    

}

extension DetailViewController: UITableViewDelegate, UITableViewDataSource {
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        delegate?.comicWillEndDragging(scrollView)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return detailStatic != nil ? 4:0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (section == 1 && detailStatic?.otherWorks.count == 0) ? 0 : 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            let model = detailStatic
            return DescriptionCell.height(model)
        } else if indexPath.section == 3{
            return 200
        } else {
            return 44
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "DescriptionCell", for: indexPath) as! DescriptionCell
            cell.model = detailStatic
            return cell
        }else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "OtherWorkCell", for: indexPath) as! OtherWorkCell
            cell.model = detailStatic
            return cell
        }else if indexPath.section == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TicketCell", for: indexPath) as! TicketCell
            cell.model = detailRealtime
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "LoveComicsCell", for: indexPath) as! LoveComicsCell
            cell.model = guessLike
            cell.didSelectClosure({ [weak self] (model) in
                let vc = ComicsDetailViewController(comicid: model.comic_id)
                self?.navigationController?.pushViewController(vc, animated: true)
            })
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return (section == 1 && detailStatic?.otherWorks.count == 0) ? CGFloat.leastNormalMagnitude : 10
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 1 {
            let vc = OtherWorksViewController(otherWorks: detailStatic?.otherWorks)
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}








